﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.channel
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
                if (!new He.BLL.channel().Exists(this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ddlModelId.DataSource = EnumHelper.GetList(typeof(Enums.sys_model));
                ddlModelId.DataTextField = "Text";
                ddlModelId.DataValueField = "Value";
                ddlModelId.DataBind();

                ddlLang.DataSource = LangHelper.GetLangDict();
                ddlLang.DataTextField = "Value";
                ddlLang.DataValueField = "Key";
                ddlLang.DataBind();

                ddlShowEffect.DataSource = GetShowEffectDict(0);
                ddlShowEffect.DataTextField = "Value";
                ddlShowEffect.DataValueField = "Key";
                ddlShowEffect.DataBind();

                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.channel bll = new BLL.channel();
            Model.channel model = bll.GetModel(_id);
            txtName.Text = model.name;
            txtTitle.Text = model.title;
            txtSubTitle.Text = model.sub_title;
            txtManageTitle.Text = model.manage_title;
            txtManageUrl.Text = model.manage_url;
            txtMaxLayer.Text = model.max_layer.ToString();
            txtPageSize.Text = model.page_size.ToString();
            ddlListEffect.SelectedValue = model.list_effect.ToString();
            ddlOrderBy.SelectedValue = model.order_by;
            ddlShowEffect.SelectedValue = model.show_effect;
            txtBindId.Text = model.bind_id;
            txtImgWidth.Text = model.img_width.ToString();
            txtImgHeight.Text = model.img_height.ToString();
            ddlModelId.SelectedValue = model.model_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            chkLock.Checked = model.is_lock == 1 ? true : false;
            chkShowAttrfield.Checked = model.show_attrfield == 1 ? true : false;
            chkIsThumb.Checked = model.is_thumb == 1 ? true : false;
            chkCategory.Checked = model.show_category == 1 ? true : false;
            chkShowCategoryImg.Checked = model.show_category_img == 1 ? true : false;
            ddlLang.SelectedValue = model.lang;
            ltrCategory.Text = string.Format("<a href='../settings/category_list.aspx?channel_id={0}' target='_blank'>分类管理</a>", model.id);

            txtSeoKeywords.Text = model.seo_keywords;
            txtSeoDescription.Text = model.seo_desc;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.channel model = new Model.channel();
            BLL.channel bll = new BLL.channel();
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.sub_title = txtSubTitle.Text.Trim();
            model.manage_title = txtManageTitle.Text.Trim();
            model.manage_url = txtManageUrl.Text.Trim();
            model.max_layer = int.Parse(txtMaxLayer.Text.Trim());
            model.page_size = int.Parse(txtPageSize.Text.Trim());
            model.list_effect = int.Parse(ddlListEffect.SelectedValue);
            model.order_by = ddlOrderBy.SelectedValue;
            model.show_effect = ddlShowEffect.SelectedValue;
            model.bind_id = txtBindId.Text.Trim();
            model.img_width = int.Parse(txtImgWidth.Text.Trim());
            model.img_height = int.Parse(txtImgHeight.Text.Trim());
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;
            model.show_attrfield = chkShowAttrfield.Checked ? 1 : 0;
            model.is_thumb = chkIsThumb.Checked ? 1 : 0;
            model.show_category = chkCategory.Checked ? 1 : 0;
            model.show_category_img = chkShowCategoryImg.Checked ? 1 : 0;
            model.lang = ddlLang.SelectedValue;

            //SEO信息
            model.seo_title = "";
            model.seo_keywords = txtSeoKeywords.Text.Trim();
            model.seo_desc = txtSeoDescription.Text.Trim();

            //保存
            if (bll.Add(model) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.channel bll = new He.BLL.channel();
            Model.channel model = bll.GetModel(_id);
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.sub_title = txtSubTitle.Text.Trim();
            model.manage_title = txtManageTitle.Text.Trim();
            model.manage_url = txtManageUrl.Text.Trim();
            model.max_layer = int.Parse(txtMaxLayer.Text.Trim());
            model.page_size = int.Parse(txtPageSize.Text.Trim());
            model.list_effect = int.Parse(ddlListEffect.SelectedValue);
            model.order_by = ddlOrderBy.SelectedValue;
            model.show_effect = ddlShowEffect.SelectedValue;
            model.bind_id = txtBindId.Text.Trim();
            model.img_width = int.Parse(txtImgWidth.Text.Trim());
            model.img_height = int.Parse(txtImgHeight.Text.Trim());
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;
            model.show_attrfield = chkShowAttrfield.Checked ? 1 : 0;
            model.is_thumb = chkIsThumb.Checked ? 1 : 0;
            model.show_category = chkCategory.Checked ? 1 : 0;
            model.show_category_img = chkShowCategoryImg.Checked ? 1 : 0;
            model.lang = ddlLang.SelectedValue;

            //SEO信息
            model.seo_title = "";
            model.seo_keywords = txtSeoKeywords.Text.Trim();
            model.seo_desc = txtSeoDescription.Text.Trim();

            if (!bll.Update(model))
            {
                return false;
            }
            //更新缓存
            CacheHelper.Remove("channel_" + model.id);

            return true;
        }
        #endregion


        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", "list.aspx?lang=" + lang, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "list.aspx?lang=" + lang, "Success");
            }
        }
    }
}